package com.neuedu.controller;

import com.neuedu.common.BaseContext;
import com.neuedu.common.Result;
import com.neuedu.dto.ShoppingCartDto;
import com.neuedu.pojo.Goods;
import com.neuedu.pojo.GoodsSpec;
import com.neuedu.pojo.ShoppingCart;
import com.neuedu.service.GoodsService;
import com.neuedu.service.GoodsSpecService;
import com.neuedu.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/shoppingCart")
@RestController
@CrossOrigin
public class ShoppingCartController_Reids {

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsSpecService goodsSpecService;

    /**
     * 向购物车中添加商品
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping
    public Result<String> addToShoppingCart(@RequestBody ShoppingCart shoppingCart){

//        Long specId = shoppingCart.getSpecId();
//        GoodsSpec goodsSpec = goodsSpecService.getById(specId);
//
//        // 在购物车中查找是否有此specId对应的商品，如果存在则更新数量即可
//        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ShoppingCart::getSpecId, shoppingCart.getSpecId());
//        ShoppingCart shoppingCartTmp = shoppingCartService.getOne(queryWrapper);
//        if(shoppingCartTmp != null){
//            Integer number = shoppingCartTmp.getNumber();
//            number += shoppingCart.getNumber();
//            shoppingCartTmp.setNumber(number);
//            shoppingCartService.updateById(shoppingCartTmp);
//            return Result.success("添加购物车成功");
//        }
//
//        Long goodsId = goodsSpec.getGoodsId();
//        shoppingCart.setGoodsId(goodsId);
//        boolean isAdded = shoppingCartService.save(shoppingCart);
//        if(isAdded)
//            return Result.success("成功添加到购物车");
//        else
            return Result.error("添加购物车失败");
    }

    /**
     * 查询购物车列表
     *
     * @return
     */
    @GetMapping
    public Result<List<ShoppingCartDto>> getAll(){
        //获取当前用户id
        Long id = BaseContext.getCurrentId();

        //条件构造器
//        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<ShoppingCart>();
//        lambdaQueryWrapper.eq(ShoppingCart::getCreateUser, id);
//
//        List<ShoppingCart> list = new ArrayList<ShoppingCart>();
//        list = shoppingCartService.list(lambdaQueryWrapper);
//
//        List<ShoppingCartDto> shoppingCartDtos = list.stream().map((item) -> {
//            ShoppingCartDto shoppingCartDto = new ShoppingCartDto();
//            BeanUtils.copyProperties(item, shoppingCartDto);
//            Goods goods = goodsService.getById(item.getGoodsId());
//            shoppingCartDto.setName(goods.getName());
//            GoodsSpec goodsSpec = goodsSpecService.getById(item.getSpecId());
//            shoppingCartDto.setImg(goodsSpec.getImg());
//            shoppingCartDto.setPrice(goodsSpec.getPrice());
//            shoppingCartDto.setSpecValue(goodsSpec.getSpecValue());
//            return shoppingCartDto;
//        }).collect(Collectors.toList());
        List<ShoppingCartDto> list = new ArrayList<>();
        String key = "shoppingCart:"+id;
        Map<Object, Object> map = redisTemplate.opsForHash().entries(key);
        for (Map.Entry<Object, Object> entry : map.entrySet()) {
            Long goods_id = (Long) entry.getKey();
            String goodsKey = "goods:"+goods_id;
            Goods goods = (Goods) redisTemplate.opsForValue().get(goodsKey);
            ShoppingCartDto dto = new ShoppingCartDto();
            dto.setName(goods.getName());
            dto.setImg();
        }
        return Result.success(shoppingCartDtos);
    }

    /**
     *
     * 查询购物车某个记录
     * @param id
     *
     */
    @GetMapping("/{id}")
    public Result<ShoppingCartDto> getOneById(@PathVariable Long id){

        ShoppingCartDto shoppingCartDto = new ShoppingCartDto();
        ShoppingCart shoppingCart = shoppingCartService.getById(id);
        BeanUtils.copyProperties(shoppingCart, shoppingCartDto);

        Long goodsId = shoppingCart.getGoodsId();
        Goods goods = goodsService.getById(goodsId);
        String name = goods.getName();
        shoppingCartDto.setName(name);

        GoodsSpec goodsSpec = goodsSpecService.getById(shoppingCart.getSpecId());
        shoppingCartDto.setPrice(goodsSpec.getPrice());
        shoppingCartDto.setImg(goodsSpec.getImg());

        return Result.success(shoppingCartDto);
    }




    /**
     * 移除购物车中的某商品
     * @param ids
     * @return
     *
     */
    @DeleteMapping
    public Result<String> deleteOneById(@RequestParam List<Long> ids){

        boolean isDeleted = shoppingCartService.removeByIds(ids);
        if(isDeleted)
            return Result.success("成功移除");
        else
            return Result.error("移除失败");
    }

    /**
     * 更改购物车中某商品的数量
     *
     * @param shoppingCart
     * @return
     */
    @PutMapping
    public Result<String> changeNum(@RequestBody ShoppingCart shoppingCart){

        Integer num = shoppingCart.getNumber();
        //检测数量是否为0
        if(num == 0){
            shoppingCartService.removeById(shoppingCart.getId());
            return Result.success("修改成功");
        }
        boolean isUpdated = shoppingCartService.updateById(shoppingCart);

        if(isUpdated)
            return Result.success("成功更改数量");
        else
            return Result.error("更改数量失败");


    }



}
